﻿<%@ Import Namespace="System.Runtime.InteropServices" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="HHG.Common" %>
<%@ Page Language="C#"   %>

<script runat="server">

	protected void Page_Load(object sender, EventArgs e)
	{
		var pUrl = Request.QueryString["purl"];

		var page =int.Parse( Request.QueryString["page"]);
		var limit =int.Parse(  Request.QueryString["limit"]);

		if (string.IsNullOrWhiteSpace(pUrl))
			pUrl = "0";
		var config = GetCrawlerConfig();


		var videos = new List<Video>();
		var files = System.IO.Directory.GetFiles(config.VideosPath);
		foreach (var item in files)
		{
			var json = System.IO.File.ReadAllText(item);
			var tempVideos = json.DeserializeObject<List<Video>>();
			videos.AddRange(tempVideos.Where(s=>s.PUrl==pUrl));
		}
		Response.ContentType = "application/json";


		var data = new {code=0,msg="",count=videos.Count,data=videos.Skip((page-1)*limit).Take(limit)};
		Response.Write(data.SerializeObject());
		Response.End();

	}
	protected void GetKeys() {
		var config = GetCrawlerConfig();

		Response.Write(System.IO.Directory.GetFiles(config.VideosPath))
			;
		Response.End();


	}

	private CrawlerConfig GetCrawlerConfig()
	{
		var filePath = Server.MapPath("~/Config/config.json");
		return System.IO.File.ReadAllText(filePath, Encoding.GetEncoding("gbk")).DeserializeObject<CrawlerConfig>();

	}
	public class CrawlerConfig
	{
		public string CrawlerExe { get; set; }
		public string VideosPath { get; set; }

	}

	public class Video
	{
		public VideoType VideoType { get; set; }
		public string Title { get; set; }
		public string Url { get; set; }
		public string PUrl { get; set; }
	}

	public enum VideoType
	{
		/// <summary>
		/// 专辑（多） 通常是连续剧
		/// </summary> 

		multiEpisode,
		/// <summary>
		/// 视频 具体的视频  比如电影  或连续剧的其中一集
		/// </summary>

		video
	}
</script>


